package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.MchntBaseInfoUpdateRequest;
import cc.rengu.igas.bomp.facade.response.MchntBaseInfoUpdateResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户基本信息更新服务
 */
public class MchntBaseInfoUpdateService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntBaseInfoUpdateRequest request = new MchntBaseInfoUpdateRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntBaseInfoUpdateRequest mchntBaseInfoUpdateRequest = (MchntBaseInfoUpdateRequest) request;
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        BizResponse bizResponse = new BizResponse();
        MchntBaseInfoUpdateResponse response = new MchntBaseInfoUpdateResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        response.setHeader(mchntBaseInfoUpdateRequest.getHeader());
        String instId = mchntBaseInfoUpdateRequest.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(instId)) {
            rglog.error("instId法人号为空！", instId);
            bizResponse.setRespCode(RespCodeEnum.PARAM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        String id = mchntBaseInfoUpdateRequest.getId();

        if (StringUtil.isEmptyOrNull(id)) {
            MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
            BeanUtil.beanCopy(mchntBaseInfoUpdateRequest, mchntBaseInfo);
            int i = mchntBaseInfoMapper.updateMchntBaseInfoById(mchntBaseInfo);
            if (i != 0) {
                rglog.error("商户信息更新失败");
                bizResponse.setRespCode(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            }
            response.setMsg("商户信息更新成功");
        }
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntBaseInfoUpdateResponse mchntBaseInfoUpdateResponse = (MchntBaseInfoUpdateResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntBaseInfoUpdateResponse);
    }
}
